package com.szrt.easyui.buybuyup.user.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szrt.easyui.buybuyup.company.dto.CompanyDTO;
import com.szrt.easyui.buybuyup.user.entity.UserEntity;
import com.szrt.easyui.buybuyup.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	/**
	 * @Resource JSR-250  导包
	 * @Reject   JSR-330  导包
	 */
	@Autowired 
	private UserService service;
	
	
	/**
	 * ids  11,12,13
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/deleteByIds")
	@ResponseBody
	public Map<String,Object> deleteByIds(@RequestBody List<Integer> ids){
		
		//String[] idss =ids.split(",");
		Map<String,Object> reuslt = new HashMap<String,Object>();
		
		service.deleteByIds(ids,reuslt);
		return reuslt;
	}
	
	@RequestMapping(value="/add")
	@ResponseBody
	public Map<String,Object> add(UserEntity entity){
		Map<String,Object> reuslt = new HashMap<String,Object>();
		
		service.add(entity,reuslt);
		return reuslt;
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public Map<String,Object> update(UserEntity entity){
		Map<String,Object> reuslt = new HashMap<String,Object>();
		
		service.update(entity,reuslt);
		return reuslt;
	}
	
	
	@RequestMapping(value="/findById")
	@ResponseBody
	public UserEntity findById(Integer id){
		
		UserEntity entity =service.findById(id);
		return entity;
	}
	
	
	@RequestMapping(value="/findAllKeyAndValue")
	@ResponseBody
	public List<CompanyDTO> findAllKeyAndValue(){
		
		List<CompanyDTO> list = new ArrayList<CompanyDTO>();
		
		list=service.findAllKeyAndValue();
		return list;
	}
	
	
	/**
	 * 分页  
	 * 
	 * 1:告诉前台我数据库中一种有多少条记录
	 * 2:告诉前台我给的是第几页的数据 并且告诉前台这一页有多少条
	 * 
	 * pageNumber page  第几页  默认是第一页 1
	 * pageSize   rows  一页有多少条数据    3
	 * 
	 *  -- limit 0,3       求0= 3*(1-1)
  		-- limit 3,3     求3= 3*(2-1)
  						  pageSize*(pageNumber-1)
  						
  
	 * findAllPage
	 * @return
	 */
	@RequestMapping(value="/findAll")
	@ResponseBody
	public Map<String,Object> findAll(Integer page,Integer rows,Integer phone){
		
		System.out.println("11111111");
		
		Integer pageNumber = page;
		Integer pageSize   = rows;
		
		Integer before =pageSize*(pageNumber-1) ;
		Integer after  =pageSize;
		
		
		//用来封装分页的数据  并且把这个数据传递给servcie
		Map<String,Object> fenye=new HashMap<String,Object>();
		
		fenye.put("before", before);
		fenye.put("after", after);
		
		if(null != phone){
			fenye.put("phone", "%"+phone+"%");
		}
		System.out.println(pageNumber + " : "+ pageSize);
		
		//用来接收Service执行查询之后返回的结果
		Map<String,Object> reuslt = new HashMap<String,Object>();
		service.findAllPage(fenye,reuslt);
		return reuslt;
		
	}
	@RequestMapping(value="/findAllPageV2")
	@ResponseBody
	public Map<String,Object> findAllPageV2(Integer page,Integer rows,Integer phone){
		
		System.out.println("11111111");
		
		Integer pageNumber = page;
		Integer pageSize   = rows;
		
		Integer before =pageSize*(pageNumber-1) ;
		Integer after  =pageSize;
		
		
		//用来封装分页的数据  并且把这个数据传递给servcie
		Map<String,Object> fenye=new HashMap<String,Object>();
		
		fenye.put("before", before);
		fenye.put("after", after);
		
		if(null != phone){
			fenye.put("phone", "%"+phone+"%");
		}
		System.out.println(pageNumber + " : "+ pageSize);
		
		//用来接收Service执行查询之后返回的结果
		Map<String,Object> reuslt = new HashMap<String,Object>();
		service.findAllPageV2(fenye,reuslt);
		return reuslt;
		
	}

}
